<?php

namespace App\Service\Index;

use App\Constants\ErrorCode;
use App\Exception\ApiException;
use App\Model\CallBase;
use App\Model\User;
use App\Model\UserBlacklist;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use App\Service\BaseService;
use App\Service\CallBaseService;
use App\Service\ServiceTrait\GetUserInfoTrait;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;

class InviteVideoCallService extends CallBaseService
{
    use GetUserInfoTrait;

    /**
     * 被拨打者的id
     * @var
     */
    private $hostUserId;

    /**
     * 被拨打者的资料
     * @var Builder|Model|object|null
     */
    private $hostProfile;

    /**
     * 主控制器
     * @return array
     */
    public function handle(): array
    {
        //自己的ID
        $this->userId = $this->getUserId();

        //男用户ID
        $this->hostUserId = $this->request->input('host_user_id', 0);

        //不能打给自己
        if ($this->userId == $this->hostUserId) {
            throw new ApiException('不能拨打给自己~',ErrorCode::PARAMETER_ERROR);
        }

        //检测是不是客服账号
        if (User::checkOfficial($this->hostUserId)) {
            throw new ApiException('暂无法拨打给小助手~',ErrorCode::PARAMETER_ERROR);
        }

        $callFrom = CallBase::FROM_INVITE_CALL;

        $this->checkCallAuthority(VideoCall::CALL_TYPE);
        $userInfo = $this->getUserInfo($this->userId);

        //修改以前无效状态
        VideoCall::finishOldCall($this->userId);

//        list($this->userId,$this->hostUserId) = array($this->hostUserId,$this->userId);

        //添加通话记录
        VideoCall::addRecord($this->userId, $this->hostUserId, $callFrom, $userInfo->video_coin_setting,3);

        //呼叫回调
        $this->callHook($this->hostUserId, $this->userId, VideoCall::CALL_TYPE);
        $ret = [
            'permit' => 1,
            'user_info' => UserProfile::buildHostItem($this->hostProfile),
            'support_face' => User::checkFaceSupport($this->hostUserId),
        ];
        log_user_result($this->userId, "ChatUserId:{$this->hostUserId}==" . json_encode($ret));
        return $ret;
    }

    /**
     * 检测用户的通话权限
     * @param $callType
     * @return void
     */
    public function checkCallAuthority($callType){
        $this->hostProfile = UserProfile::getProfile($this->hostUserId);

        //拉黑关系
        $checkBlack = UserBlacklist::check($this->hostUserId, $this->userId);
        if ($checkBlack) {
            throw new ApiException('暂无法发起通话~',ErrorCode::CHAT_BLACK_LMT);
//            return $this->setError(ErrorCode::CHAT_BLACK_LMT, '暂无法发起通话~');
        }

        //对方是否允许接听，或者对方正在通话
        $permitField = $callType == VoiceCall::CALL_TYPE ? 'call_permit' : 'video_permit';
        if ($this->hostProfile->{$permitField} == 0) {
            throw new ApiException('对方忙或手机不在身边，请稍后再试',ErrorCode::HOST_BUSY);
//            return $this->setError(ErrorCode::HOST_BUSY, '对方忙或手机不在身边，请稍后再试');
        }

        //对方是否在打语音电话
        if (
            VoiceCall::checkUserChatting($this->hostUserId) ||
            VideoCall::checkUserChatting($this->hostUserId)
        ) {
            throw new ApiException('对方忙或手机不在身边，请稍后再试',ErrorCode::HOST_BUSY);
//            return $this->setError(ErrorCode::HOST_BUSY, '对方忙或手机不在身边，请稍后再试');
        }

        // 判断对方用户状态
        $user = User::getUser($this->hostUserId);
        if ($user && $user->status != User::STATUS_OK) {
            throw new ApiException('对方暂无法接听',ErrorCode::DEFAULT_ERROR);
//            return $this->setError(ErrorCode::DEFAULT_ERROR, '对方暂无法接听');
        }
    }
}